package com.quercus.servlet;

import com.quercus.mapper.UserMapper;
import com.quercus.pojo.User;
import com.quercus.util.SqlsessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*接受前台页面中的数据信息*/
        String uname = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        /*数据处理，连接数据库进行数据比较返回结果*/
        SqlSession sqlSession = SqlsessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectOne(uname,pwd);
        /*根据返回结果给用户作出响应*/
        if(user!=null){
            /*证明当前用户存在*/
            resp.sendRedirect(req.getContextPath()+"/success.jsp");
        }else{
            /*登录失败*/
            req.setAttribute("msg","Fail!");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }
}
